/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qdeclarativesparticles_p.h"
#include "qdeclarativesparticles.h"

#include <qdeclarativeinfo.h>


#include <private/qdeclarativepixmapcache_p.h>
#include <QtCore/QAbstractAnimation>

#include <QPainter>
#include <QtGui/qdrawutil.h>
#include <QVarLengthArray>

#include <stdlib.h>
#include <math.h>

#ifndef M_PI
#define M_PI 3.14159265358979323846
#define M_PI_2 (M_PI / 2.)
#endif
#ifndef INT_MAX
#define INT_MAX 2147483647
#endif

QT_BEGIN_NAMESPACE


#define PI_SQR 9.8696044
// parabolic approximation
inline qreal fastSin(qreal theta)
{
    const qreal b = 4 / M_PI;
    const qreal c = -4 / PI_SQR;

    qreal y = b * theta + c * theta * qAbs(theta);
    return y;
}

inline qreal fastCos(qreal theta)
{
    theta += M_PI_2;
    if (theta > M_PI)
        theta -= 2 * M_PI;

    return fastSin(theta);
}

void QDeclarativeSParticlesPrivate::tick(int time)
{
    Q_Q(QDeclarativeSParticles);
//    if (!motion)
//        motion = new QDeclarativeParticleMotionLinear(q);

//    int oldCount = particles.count();
//    int removed = 0;
    int interval = time - lastAdvTime;
//    for (int i = 0; i < particles.count(); ) {
//        QDeclarativeParticle &particle = particles[i];
//        int age = time - particle.birthTime;
//        if (age >= particle.lifeSpan)  {
//            QDeclarativeParticle part = particles.takeAt(i);
//            motion->destroy(part);
//            ++removed;
//        } else {
//            updateOpacity(particle, age);
//            motion->advance(particle, interval);
//            ++i;
//        }
//    }

//    if(emissionRate == -1)//Otherwise leave emission to the emission rate
//        while(removed-- && ((count == -1) || particles.count() < count))
//            createParticle(time);

//    if (!addParticleTime)
//        addParticleTime = time;

//    //Possibly emit new particles
//    if (((count == -1) || particles.count() < count) && emissionRate
//            && !(count==-1 && emissionRate==-1)) {
//        int emissionCount = -1;
//        if (emissionRate != -1){
//            qreal variance = 1.;
//            if (emissionVariance > 0.){
//                variance += (qreal(qrand())/RAND_MAX) * emissionVariance * (qrand()%2?-1.:1.);
//            }
//            qreal emission = emissionRate * (qreal(interval)/1000.);
//            emission = emission * variance + emissionCarry;
//            double tmpDbl;
//            emissionCarry = modf(emission, &tmpDbl);
//            emissionCount = (int)tmpDbl;
//            emissionCount = qMax(0,emissionCount);
//        }
//        while(((count == -1) || particles.count() < count) &&
//                (emissionRate==-1 || emissionCount--))
//            createParticle(time);
//    }

//    //Deal with emissions from requested bursts
//    for(int i=0; i<bursts.size(); i++){
//        int emission = 0;
//        if(bursts[i].second == -1){
//            emission = bursts[i].first;
//        }else{
//            qreal variance = 1.;
//            if (emissionVariance > 0.){
//                variance += (qreal(qrand())/RAND_MAX) * emissionVariance * (qrand()%2?-1.:1.);
//            }
//            qreal workingEmission = bursts[i].second * (qreal(interval)/1000.);
//            workingEmission *= variance;
//            emission = (int)workingEmission;
//            emission = qMax(emission, 0);
//        }
//        emission = qMin(emission, bursts[i].first);
//        bursts[i].first -= emission;
//        while(emission--)
//            createParticle(time);
//    }
//    for(int i=bursts.size()-1; i>=0; i--)
//        if(bursts[i].first <= 0)
//            bursts.removeAt(i);

    lastAdvTime = time;
    paintItem->updateSize();
    paintItem->update();
//    if (!(oldCount || particles.count()) && (!count || !emissionRate) && bursts.isEmpty()) {
        lastAdvTime = 0;
        clock.stop();
//    }
}

QDeclarativeSParticles::QDeclarativeSParticles(QDeclarativeItem *parent)
    : QDeclarativeItem(*(new QDeclarativeSParticlesPrivate), parent)
{
    Q_D(QDeclarativeSParticles);
    d->init();
}

QDeclarativeSParticles::~QDeclarativeSParticles()
{
}

void QDeclarativeSParticles::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
{
    Q_UNUSED(p);
    //painting is done by the ParticlesPainter, so it can have the right size
}



void QDeclarativeSParticles::componentComplete()
{
    Q_D(QDeclarativeSParticles);
    QDeclarativeItem::componentComplete();
    if (d->count && d->emissionRate) {
        d->paintItem->updateSize();
        d->clock.start();
    }
    if (d->lifeSpanDev > d->lifeSpan)
        d->lifeSpanDev = d->lifeSpan;
}

QT_END_NAMESPACE
